package com.lx.huimailf.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import com.lx.huimailf.R;
import com.lx.huimailf.configs.Config;
import com.lx.huimailf.utils.DateTimePickDialogUtil;
import com.lx.huimailf.utils.TestDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class MoneyDetialActivity extends Activity {
    RelativeLayout fanhui;
    ListView lv;
    TextView shaixuan;
    EditText danjuhao;
    List<Map<String, String>> list=new ArrayList<Map<String,String>>();
    private String custid;
    String str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_moneydetial);
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        shaixuan=(TextView)findViewById(R.id.shaixuan);
        lv=(ListView)findViewById(R.id.lv);
        SimpleDateFormat formatter = new SimpleDateFormat    ("yyyy年MM月dd日 HH:mm ");
        Date curDate =new Date(System.currentTimeMillis());//获取当前时间
        str = formatter.format(curDate);
        SharedPreferences sharedPreferences=getSharedPreferences("user",Context.MODE_PRIVATE);
        custid=sharedPreferences.getString("custid","-1111");
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MoneyDetialActivity.this.finish();
            }
        });
        shaixuan.setOnClickListener(new View.OnClickListener() {

            private String initStartDateTime = str; // 初始化开始时间
            private String initEndDateTime = str; // 初始化结束时间
            private AlertDialog dialog;
            private TextView tv1;
            private TextView tv2;
            private  Button ok;
            private  Button cancel;
            private List<String> list = new ArrayList<String>();
            private TextView myTextView;
            private Spinner mySpinner;
            private ArrayAdapter<String> adapter;
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.dialog_select,
                        (ViewGroup)findViewById(R.id.dialog_select));
                AlertDialog.Builder builder = new AlertDialog.Builder(MoneyDetialActivity.this);
                builder .setView(layout);
                dialog=builder.create();
                dialog.show();
                tv1=(TextView)layout.findViewById(R.id.tv1);
                tv2=(TextView) layout.findViewById(R.id.tv2);
                danjuhao=(EditText)layout.findViewById(R.id.danjuhao);
                ok=(Button)layout.findViewById(R.id.ok);
                cancel=(Button)layout.findViewById(R.id.cancel);
                tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TestDialog dialog  = new TestDialog(MoneyDetialActivity.this, R.style.Dialog);
                        dialog.setTitle("选择时间");
                        dialog.setBackButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setConfirmButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tv1.setText(TestDialog.getDate());
                            }
                        });
                        dialog.show();

                    }
                });
                tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TestDialog dialog  = new TestDialog(MoneyDetialActivity.this, R.style.Dialog);
                        dialog.setTitle("选择时间");
                        dialog.setBackButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setConfirmButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tv2.setText(TestDialog.getDate());
                            }
                        });
                        dialog.show();


                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap<String,String> map=new HashMap<String, String>();
                        map.put("distributeid",custid);
                        map.put("page","1");
                        map.put("rows","20");
                        map.put("createtimeGE",tv1.getText().toString());
                        map.put("createtimeLE",tv2.getText().toString());
                        map.put("orderno",danjuhao.getText().toString());
                        Gson gson=new Gson();
                        String str=gson.toJson(map).toString();
                        OkHttpUtils
                                .post()
                                .url(Config.url+"fees/searchfees.do")
                                .addParams("data", str)
                                .addParams("mobile","true")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        Toast.makeText(MoneyDetialActivity.this,"服务器连接失败",Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        System.out.print("佣金筛选--"+response);
                                        try{
                                            JSONObject o1=new JSONObject(response);
                                            JSONArray a1=o1.getJSONArray("list");
                                            List<Map<String, String>> lists=new ArrayList<Map<String,String>>();
                                            if (a1.length()>0) {
                                                for (int i = 0; i < a1.length(); i++) {
                                                    JSONObject o3 = a1.getJSONObject(i);
                                                    Map<String, String> map = new HashMap<String, String>();
                                                    map.put("createtime", getData(o3.getString("createtime")));
                                                    map.put("orderno", o3.getString("orderno"));
                                                    map.put("distributeprofit", o3.getString("distributeprofit"));
                                                    lists.add(map);

                                                }
                                                PoiAdapter adapter = new PoiAdapter(MoneyDetialActivity.this, lists);
                                                lv.setAdapter(adapter);
                                            }else{
                                                lv.setVisibility(View.GONE);
                                            }
                                        }catch(JSONException e){
                                            e.printStackTrace();
                                        }
                                        dialog.dismiss();
                                    }

                                });
                    }
                });
            }
        });
        get();
    }

    private void get(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("distributeid",custid);
        map.put("page","1");
        map.put("rows","20");
        Gson gson=new Gson();
        String str=gson.toJson(map).toString();
        OkHttpUtils
                .post()
                .url(Config.url+"fees/searchfees.do")
                .addParams("data", str)
                .addParams("mobile","true")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(MoneyDetialActivity.this,"服务器连接失败",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.print("佣金列表--"+response);
                        list = new ArrayList<Map<String, String>>();
                        try{
                            JSONObject o1=new JSONObject(response);
                            JSONArray a1=o1.getJSONArray("list");
                            for(int i=0;i<a1.length();i++){
                                JSONObject o3=a1.getJSONObject(i);
                                Map<String,String> map=new HashMap<String,String>();
                                map.put("createtime",getData(o3.getString("createtime")));
                                map.put("orderno", o3.getString("orderno"));
                                map.put("distributeprofit", o3.getString("distributeprofit"));
                                list.add(map);

                            }
                            PoiAdapter adapter = new PoiAdapter(MoneyDetialActivity.this,list);
                            lv.setAdapter(adapter);
                        }catch(JSONException e){
                            e.printStackTrace();
                        }
                    }

                });
    }



    public class PoiAdapter extends BaseAdapter {
        private Context context;
        List<Map<String, String>> List;
        private LayoutInflater mInflater;
        public PoiAdapter(Context context,List<Map<String, String>> List){
            this.List = List;
            this.context = context;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return List.size();
        }
        @Override
        public Object getItem(int position) {
            return List.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_moneydatial, null);
                holder.dingdanhao = (TextView)convertView.findViewById(R.id.dingdanhao);
                holder.lirun = (TextView)convertView.findViewById(R.id.lirun);
                holder.time = (TextView)convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.dingdanhao.setText(List.get(position).get("orderno"));
            holder.lirun.setText(List.get(position).get("distributeprofit"));
            holder.time.setText(List.get(position).get("createtime"));

            return convertView;

        }
    }
    public final class ViewHolder{

        public TextView dingdanhao;
        public TextView lirun;
        public TextView time;

    }
    @SuppressWarnings("unused")
    private String getData(String timeString){
        Long time = Long.parseLong(timeString);
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));
        return date;
    }
}
